﻿using BenchmarkDotNet.Attributes;
using KdbndpTest.Models;
using SqlSugar;

namespace KdbndpTest.BenchmarkRunner
{

    [MemoryDiagnoser]
    public class Demo1_Insertable
    {
        [Benchmark(Description = "正常新增表单数据")]
        public void Insert()
        {
            var insertObj = new Order() { Name = "order1", Price = 0 };
            var updateObjs = new List<Order> {
                 new Order() { Id = 11, Name = "order11", Price=0 },
                 new Order() { Id = 12, Name = "order12" , Price=0}
            };

            var db = GetInstance();

            //Ignore  CreateTime
            var insertId = db.Insertable(insertObj).IgnoreColumns(it => new { it.CreateTime }).ExecuteReturnIdentity();//get identity

            db.Deleteable<Order>().In(insertId).ExecuteCommand();

        }

        [Benchmark(Description = "新增指定字段")]
        public void InsertColumns()
        {
            var insertObj = new Order() { Name = "order1", Price = 0 };
            var updateObjs = new List<Order> {
                 new Order() { Id = 11, Name = "order11", Price=0 },
                 new Order() { Id = 12, Name = "order12" , Price=0}
            };

            var db = GetInstance();

            //Only  insert  Name and Price
            var insertId = db.Insertable(insertObj).InsertColumns(it => new { it.Name, it.Price }).ExecuteReturnIdentity();

            db.Deleteable<Order>().In(insertId).ExecuteCommand();
        }

        //public void 新增数据库没有字段测试()
        //{
        //    var insertObj = new Order() { Name = "order1", Price = 0 };
        //    var updateObjs = new List<Order> {
        //         new Order() { Id = 11, Name = "order11", Price=0 },
        //         new Order() { Id = 12, Name = "order12" , Price=0}
        //    };

        //    var db = GetInstance();

        //    //ignore null columns
        //    db.Insertable(updateObjs).ExecuteCommand();//get change row count
        //}

        [Benchmark(Description = "新增并发测试")]
        public void InsertUpdLock()
        {
            var insertObj = new Order() { Name = "order1", Price = 0 };

            var db = GetInstance();

            //Use Lock
            var insertId = db.Insertable(insertObj).With(SqlWith.UpdLock).ExecuteCommand();

            db.Deleteable<Order>().In(insertId).ExecuteCommand();
        }

        private static SqlSugarClient GetInstance()
        {
            var db = new SqlSugarClient(new ConnectionConfig()
            {
                DbType = SqlSugar.DbType.Kdbndp,
                ConnectionString = Config.ConnectionString,
                InitKeyType = InitKeyType.Attribute,
                IsAutoCloseConnection = true,
                MoreSettings = new ConnMoreSettings()
                {
                    DatabaseModel = DbType.PostgreSQL
                },
                AopEvents = new AopEvents
                {
                    OnLogExecuting = (sql, p) =>
                    {
                        Console.WriteLine(sql);
                        Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
                    }
                }
            });

            //If no exist create datebase 
            db.DbMaintenance.CreateDatabase();

            //Use db query
            var dt = db.Ado.GetDataTable("select 1");

            //Create tables
            db.CodeFirst.InitTables(typeof(OrderItem), typeof(Order));

            return db;
        }
    }
}
